package com.qixin.modules.eaApplication.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.qixin.modules.ea.entity.EaEntertainReceptionActivity;
import com.qixin.modules.ea.entity.EaPhoneRecord;
import com.qixin.modules.ea.entity.EaSysDictItem;
import com.qixin.modules.ea.mapper.EaEntertainReceptionActivityMapper;
import com.qixin.modules.ea.mapper.EaPhoneRecordMapper;
import com.qixin.modules.ea.mapper.EaSysDictItemMapper;
import com.qixin.modules.ea.mapper.EaSysUserMapper;
import com.qixin.modules.ea.vo.EaSysUser;
import com.qixin.modules.ea.vo.ListPageVo;
import com.qixin.modules.eaApplication.entity.EaEntertainApply;
import com.qixin.modules.eaApplication.entity.EaEntertainChange;
import com.qixin.modules.eaApplication.entity.EaHostObject;
import com.qixin.modules.eaApplication.mapper.EaHostObjectMapper;
import com.qixin.modules.eaApplication.mapper.EaEntertainApplyMapper;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyService;
import com.qixin.modules.eaApplication.service.IEaEntertainApplyServiceCopy;
import com.qixin.modules.eaBasic.entity.EaRegion;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaRegionMapper;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaBasic.util.WorkFlowUtil;
import com.qixin.modules.eaBasic.vo.EaTaskVo;
import com.qixin.modules.eaBasic.vo.UserIdPageVo;
import com.qixin.modules.eaLoan.entity.EaLoanbill;
import com.qixin.modules.eaLoan.entity.EaLoanbillDetail;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.*;

/**
 * @Description: 招待费申请单
 * @Author: qixin-boot
 * @Date:   2021-07-06
 * @Version: V1.0
 */
@Service
public class EaEntertainApplyServiceImplCopy extends ServiceImpl<EaEntertainApplyMapper, EaEntertainApply> implements IEaEntertainApplyServiceCopy {
	@Autowired
	private EaEntertainApplyMapper eaEntertainApplyMapper;
	@Autowired
	private EaTaskMapper eaTaskMapper;
	@Autowired
	private ChangeIdToNameUtil changeIdToNameUtil;
	@Autowired
	private EaRegionMapper eaRegionMapper;
	@Autowired
	private EaHostObjectMapper eaHostObjectMapper;
	@Value(value = "${server.fileurl}")
	private String fileurl;
	@Autowired
	private WorkFlowUtil workFlowUtil;
	@Autowired
	private EaEntertainReceptionActivityMapper eaEntertainReceptionActivityMapper;
	@Autowired
	private EaPhoneRecordMapper eaPhoneRecordMapper;

	//新增招待费申请单
	@Override
	@Transactional
	public Map<String,Object> saveMain(EaEntertainApply eaEntertainApply,List<EaHostObject> eaHostObjectList,List<EaEntertainReceptionActivity> eaEntertainReceptionActivities,EaPhoneRecord eaPhoneRecord) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		Object result = FillRuleUtil.executeRule("entertain_code_rule", null);  //获取单据编号
		eaEntertainApply.setBillNo(result.toString());

		String saveType = eaEntertainApply.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			eaEntertainApply.setState(0);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaEntertainApply.setState(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		eaEntertainApply.setId(Uuid32Util.getUUid());  //主键
		eaEntertainApply.setIsused("0");    //是否已使用
		eaEntertainApply.setIsChanging("0");    //是否变更状态
		eaEntertainApplyMapper.insert(eaEntertainApply);

		//保存招待对象明细表
		if(eaHostObjectList != null && eaHostObjectList.size() > 0){
			for (int i = 0; i < eaHostObjectList.size(); i++) {
				EaHostObject eaHostObject = eaHostObjectList.get(i);
				eaHostObject.setId(Uuid32Util.getUUid());
				eaHostObject.setEntertainApplyId(eaEntertainApply.getId());
				eaHostObjectMapper.insert(eaHostObject);
			}
		}
		if(eaEntertainReceptionActivities!=null && eaEntertainReceptionActivities.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivities) {
				//新增活动明细
				entity.setPid(eaEntertainApply.getId());
				entity.setId(Uuid32Util.getUUid());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}

		// 保存电话记录单
		if(eaPhoneRecord != null) {
			eaPhoneRecord.setEntertainApplyId(eaEntertainApply.getId());
			eaPhoneRecord.setId(Uuid32Util.getUUid());
			eaPhoneRecordMapper.insert(eaPhoneRecord);
		}

//		//新增待办
//		EaTask eaTask = new EaTask();
//		eaTask.setId(Uuid32Util.getUUid());
//		eaTask.setFormType("6");  //单据类型:1差旅费报销单,2借款单,3还款单,4出差申请单,5出差申请变更单,6招待费申请单,7招待费申请变更单,8招待费报销单,9资金支付审批单,10通讯报销单,11电话记录单
//		eaTask.setAuditManId(eaEntertainApply.getAuditManId());   //审批人id
//		eaTask.setAuditMan(eaEntertainApply.getAuditMan());    //审批人
//		//提交类型:保存:add,提交:send
//		if(saveType.equals("add")){  //add保存,send提交
//			eaTask.setStatus("0");
//		}else{
//			eaTask.setStatus("1");
//		}
//		eaTask.setFormId(eaEntertainApply.getId());    //单据id
//		eaTask.setFormCode(eaEntertainApply.getBillNo());   //单据编号
//		eaTask.setMemo(eaEntertainApply.getCause());    //备注
//		eaTask.setTotalFee(eaEntertainApply.getAmount()); //总金额
//		eaTask.setSort("6");   //排序
//		eaTask.setTitle("招待费申请单");  //标题
//		eaTask.setSendManId(eaEntertainApply.getPersonId());    //发送人id
//		eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaEntertainApply.getPersonId()));    //发送人
//		eaTask.setSendDepartId(eaEntertainApply.getDeptId());  //发送人部门id
//		eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaEntertainApply.getDeptId()));   //发送人部门
//		eaTask.setSendTime(new Date());    //发送时间
//		eaTaskMapper.insert(eaTask);

		returnMap.put("id",eaEntertainApply.getId());
		returnMap.put("code","0");
		returnMap.put("message","保存成功!");
		return returnMap;
	}

	//编辑招待费申请单
	@Override
	@Transactional
	public Map<String,Object> updateMain(EaEntertainApply eaEntertainApply, List<EaHostObject> eaHostObjectList, List<EaEntertainReceptionActivity> eaEntertainReceptionActivities, EaPhoneRecord eaPhoneRecord) {
		Map<String,Object> returnMap = new HashMap<String,Object>();

		//如果picture中有http://localhost:9092/qixin-boot/aaa.png,要处理只保留文件名
		String picture = eaEntertainApply.getPicture() != null ? eaEntertainApply.getPicture().toString() : "";
		String picture_final = picture.replaceAll(fileurl,"");;
		//设置最终的图片如:1111.png,2222.png
		eaEntertainApply.setPicture(picture_final);

		String saveType = eaEntertainApply.getSaveType();
		if(saveType.equals("add")) {  //add保存,send提交
			EaEntertainApply eaEntertainApply1 = eaEntertainApplyMapper.selectById(eaEntertainApply.getId());
			eaEntertainApply.setState(eaEntertainApply1.getState());   //单据状态:0未提交,1审批中,2已审批,3已退回
		}else{
			eaEntertainApply.setState(1);   //单据状态:0未提交,1审批中,2已审批,3已退回
		}

		eaEntertainApplyMapper.updateById(eaEntertainApply);

		//先删除相应的招待对象明细表
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("entertain_apply_id",eaEntertainApply.getId());
		eaHostObjectMapper.deleteByMap(params);
		//删除活动明细
		Map<String,Object> activitiParams = new HashMap<String,Object>();
		activitiParams.put("pid",eaEntertainApply.getId());
		eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);
		//删除电话记录单
		Map<String,Object> phoneParams = new HashMap<String,Object>();
		phoneParams.put("entertain_apply_id",eaEntertainApply.getId());
		eaPhoneRecordMapper.deleteByMap(phoneParams);


		//再保存招待对象明细表
		if(eaHostObjectList != null && eaHostObjectList.size() > 0){
			for (int i = 0; i < eaHostObjectList.size(); i++) {
				EaHostObject eaHostObject = eaHostObjectList.get(i);
				eaHostObject.setId(Uuid32Util.getUUid());
				eaHostObject.setEntertainApplyId(eaEntertainApply.getId());
				eaHostObjectMapper.insert(eaHostObject);
			}
		}
		if(eaEntertainReceptionActivities!=null && eaEntertainReceptionActivities.size()>0) {
			for(EaEntertainReceptionActivity entity:eaEntertainReceptionActivities) {
				//新增活动明细
				entity.setPid(eaEntertainApply.getId());
				entity.setId(Uuid32Util.getUUid());
				eaEntertainReceptionActivityMapper.insert(entity);
			}
		}
		if(eaPhoneRecord != null){
			//新增电话记录单
			eaPhoneRecord.setId(Uuid32Util.getUUid());
			eaPhoneRecord.setEntertainApplyId(eaEntertainApply.getId());
			eaPhoneRecordMapper.insert(eaPhoneRecord);
		}

		//修改待办
//		Map<String,Object> paramsMap2 = new HashMap<String,Object>();
//		paramsMap2.put("form_id",eaEntertainApply.getId());
//		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap2);
//		for (EaTask eaTask:eaTaskList) {
//			eaTask.setAuditManId(eaEntertainApply.getAuditManId());   //审批人id
//			eaTask.setAuditMan(eaEntertainApply.getAuditMan());    //审批人
//			//提交类型:保存:add,提交:send
//			if(saveType.equals("add")){  //add保存,send提交
//				eaTask.setStatus("0");
//			}else{
//				eaTask.setStatus("1");
//			}
//			eaTask.setFormId(eaEntertainApply.getId());    //单据id
//			eaTask.setFormCode(eaEntertainApply.getBillNo());   //单据编号
//			eaTask.setMemo(eaEntertainApply.getCause());    //备注
//			eaTask.setTotalFee(eaEntertainApply.getAmount()); //总金额
//			eaTask.setSendManId(eaEntertainApply.getPersonId());    //发送人id
//			eaTask.setSendMan(changeIdToNameUtil.getPsnNameById(eaEntertainApply.getPersonId()));    //发送人
//			eaTask.setSendDepartId(eaEntertainApply.getDeptId());  //发送人部门id
//			eaTask.setSendDepart(changeIdToNameUtil.getNameByDepartId(eaEntertainApply.getDeptId()));   //发送人部门
//			eaTask.setSendTime(new Date());    //发送时间
//			eaTaskMapper.updateById(eaTask);
//		}

		returnMap.put("id",eaEntertainApply.getId());
		returnMap.put("code","0");
		returnMap.put("message","编辑成功!");
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String,Object> delMain(String id) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(id);
		String isused = eaEntertainApply.getIsused();  //是否被使用,1为已使用
		if(isused.equals("1")){
			returnMap.put("code","-1");
			returnMap.put("message","招待费申请单:"+eaEntertainApply.getBillNo() + "已被使用,不允许被删除!");
			return returnMap;
		}

		eaEntertainApplyMapper.deleteById(id);
		//先删除相应的招待对象明细表
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("entertain_apply_id",eaEntertainApply.getId());
		eaHostObjectMapper.deleteByMap(params);
		//删除活动明细
		Map<String,Object> activitiParams = new HashMap<String,Object>();
		activitiParams.put("pid",eaEntertainApply.getId());
		eaEntertainReceptionActivityMapper.deleteByMap(activitiParams);

		//删除电话记录单
		Map<String,Object> phoneParams = new HashMap<String,Object>();
		phoneParams.put("entertain_apply_id",eaEntertainApply.getId());
		eaPhoneRecordMapper.deleteByMap(phoneParams);

		//删除相应的待办
//		Map<String,Object> paramaMap = new HashMap<String,Object>();
//		paramaMap.put("form_id",id);
//		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramaMap);
//		if(eaTaskList != null && eaTaskList.size() > 0){
//			EaTask eaTask = eaTaskList.get(0);
//
//			//删除相应的工作流
//			workFlowUtil.deleteWorkFlowById(eaTask.getWorkflowTaskId(),eaTask.getProcinstId());
//
//			eaTaskMapper.deleteById(eaTask.getId());
//		}

		returnMap.put("code","0");
		returnMap.put("message","删除成功!");
		return returnMap;
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			eaEntertainApplyMapper.deleteById(id);
		}
	}

	//查询招待费申请单列表
	@Override
	public Map<String, Object> list(ListPageVo listPageVo) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Map<String,Object> map = new HashMap<String,Object>();
		String page = EmptyUtil.checkNull(listPageVo.getPage());
		String limit = EmptyUtil.checkNull(listPageVo.getLimit());
		String keyword = EmptyUtil.checkNull(listPageVo.getKeyword());
		String userid = EmptyUtil.checkNull(loginUser.getId());   //用户id
		String pkPsndoc = EmptyUtil.checkNull(loginUser.getPkPsndoc());   //人员主键
		String isused = EmptyUtil.checkNull(listPageVo.getIsused());   //是否已使用
		String status = EmptyUtil.checkNull(listPageVo.getStatus());   //状态
		String isChanging = EmptyUtil.checkNull(listPageVo.getIsChanging());   //变更状态
		String billNo = EmptyUtil.checkNull(listPageVo.getBillNo());   //单据编号(pc)
		String billDateBegin = EmptyUtil.checkNull(listPageVo.getBillDateBegin());   //单据日期-开始(pc)
		String billDateEnd = EmptyUtil.checkNull(listPageVo.getBillDateEnd());   //单据日期-结束(pc)
		String personId = EmptyUtil.checkNull(listPageVo.getPersonId());   //申请人/报销人/借款人/还款人(pc)
		String explainStr = EmptyUtil.checkNull(listPageVo.getCasue());   //事由(pc)
		String sysOrgCode = EmptyUtil.checkNull(listPageVo.getSysOrgCode());   //所属组织(pc)
		String isAll = listPageVo.getIsAll() != null ? listPageVo.getIsAll().toString() : "1";    //是否所有
		String keyword1 = "%" + keyword + "%";
		map.put("keyword",keyword1);
		map.put("page",(Integer.parseInt(page) -1) * Integer.parseInt(limit));
		map.put("limit",Integer.parseInt(limit));
		map.put("userid",userid);
		map.put("isused",isused);
		map.put("status",status);
		map.put("isChanging",isChanging);
		map.put("billNo",billNo);
		map.put("billDateBegin",billDateBegin);
		map.put("billDateEnd",billDateEnd);
		map.put("personId",personId);
		map.put("explainStr",explainStr);
		map.put("isAll",isAll);
		map.put("sysOrgCode",sysOrgCode);
		map.put("pkPsndoc",pkPsndoc);
		List<EaEntertainApply> eaEntertainApplyList = eaEntertainApplyMapper.selectEntertainApplys(map);

		List<Map<String,Object>> returnList = new ArrayList<Map<String,Object>>();
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() > 0){
			for (EaEntertainApply eaEntertainApply:eaEntertainApplyList) {
				Map<String,Object> returnMap = new HashMap<String,Object>();
				//将实体数据存到map
				returnMap = JSON.parseObject(JSON.toJSONString(eaEntertainApply, SerializerFeature.WriteMapNullValue), Map.class);

				//格式化时间
				Long createTime = returnMap.get("createTime") != null ? Long.parseLong(returnMap.get("createTime").toString()) : null;
				String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
				returnMap.put("createTime",createTimeStr);

				//格式化时间
				Long updateTime = returnMap.get("updateTime") != null ? Long.parseLong(returnMap.get("updateTime").toString()) : null;
				String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
				returnMap.put("updateTime",updateTimeStr);

				//更新时间-用于校验时效性
				map.put("time",updateTimeStr);

				//格式化时间
				Long receptionTime = returnMap.get("receptionTime") != null ? Long.parseLong(returnMap.get("receptionTime").toString()) : null;
				String receptionTimeStr = DateFormatUtil.formatDateyyMMdd(receptionTime);
				returnMap.put("receptionTime",receptionTimeStr);

				//格式化时间
				Long billDate = returnMap.get("billDate") != null ? Long.parseLong(returnMap.get("billDate").toString()) : null;
				String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
				returnMap.put("billDate",billDateStr);

				//格式化时间 - 用餐安排_时间
				Long mealArrangementDate = returnMap.get("mealArrangementDate") != null ? Long.parseLong(returnMap.get("mealArrangementDate").toString()) : null;
				String mealArrangementDateStr = DateFormatUtil.formatDateyyMMdd(mealArrangementDate);
				returnMap.put("mealArrangementDate",mealArrangementDateStr);

				//添加图片
				String picture = returnMap.get("picture") != null ? returnMap.get("picture").toString() : "";
				List<Map<String,Object>> fileList = GetUrlUtil.getUrl(picture,fileurl);

				//添加图片fileList
				returnMap.put("fileList",fileList);
				returnList.add(returnMap);
			}
		}

		int count = 0;
		map.put("page",0);
		map.put("limit",0);
		map.put("keyword",keyword1);
		map.put("userid",userid);
		List<EaEntertainApply> list2 = eaEntertainApplyMapper.selectEntertainApplys(map);
		count = list2.size();
		Map<String,Object> returnMap = new HashMap<String,Object>();
		returnMap.put("records",eaEntertainApplyList);
		returnMap.put("total",count);
		return returnMap;
	}

	//查询招待费申请单详情
	@Override
	public Map<String, Object> selectById(String id) {
		//查询招待费申请单详情
		Map<String,Object> queryWrapper = new HashMap<String,Object>();
		queryWrapper.put("id",id);  //招待费申请单id
		queryWrapper.put("page",0);
		queryWrapper.put("limit",0);
		List<EaEntertainApply> eaEntertainApplyList = eaEntertainApplyMapper.selectEntertainApplys(queryWrapper);
		Map<String,Object> returnMap = new HashMap<String,Object>();
		if(eaEntertainApplyList != null && eaEntertainApplyList.size() > 0){
			EaEntertainApply eaEntertainApply = eaEntertainApplyList.get(0);

			//将实体数据存到map
			returnMap = JSON.parseObject(JSON.toJSONString(eaEntertainApply, SerializerFeature.WriteMapNullValue), Map.class);

			//将申请金额转为大写金额
			String amount_cn = returnMap.get("amount") != null ? returnMap.get("amount").toString() : "0";
			amount_cn = ConvertUpMoneyUtil.toChinese(amount_cn);
			returnMap.put("amount_cn",amount_cn);

			//格式化时间
			Long createTime = returnMap.get("createTime") != null ? Long.parseLong(returnMap.get("createTime").toString()) : null;
			String createTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(createTime);
			returnMap.put("createTime",createTimeStr);

			//格式化时间
			Long updateTime = returnMap.get("updateTime") != null ? Long.parseLong(returnMap.get("updateTime").toString()) : null;
			String updateTimeStr = DateFormatUtil.formatDateyyMMddHHmmss(updateTime);
			returnMap.put("updateTime",updateTimeStr);

			//更新时间-用于校验时效性
			returnMap.put("time",updateTimeStr);

			//格式化时间
			Long receptionTime = returnMap.get("receptionTime") != null ? Long.parseLong(returnMap.get("receptionTime").toString()) : null;
			String receptionTimeStr = DateFormatUtil.formatDateyyMMdd(receptionTime);
			returnMap.put("receptionTime",receptionTimeStr);

			//格式化时间
			Long billDate = returnMap.get("billDate") != null ? Long.parseLong(returnMap.get("billDate").toString()) : null;
			String billDateStr = DateFormatUtil.formatDateyyMMdd(billDate);
			returnMap.put("billDate",billDateStr);

			//格式化时间 - 用餐安排_时间
			Long mealArrangementDate = returnMap.get("mealArrangementDate") != null ? Long.parseLong(returnMap.get("mealArrangementDate").toString()) : null;
			String mealArrangementDateStr = DateFormatUtil.formatDateyyMMdd(mealArrangementDate);
			returnMap.put("mealArrangementDate",mealArrangementDateStr);

			//添加图片
			String picture = returnMap.get("picture") != null ? returnMap.get("picture").toString() : "";
			List<Map<String,Object>> fileList = GetUrlUtil.getUrl(picture,fileurl);

			//添加图片fileList
			returnMap.put("fileList",fileList);

			//查询招待费明细信息
			Map<String,Object> eaHostObjectMap = new HashMap<String,Object>();
			eaHostObjectMap.put("entertain_apply_id",id);
			List<EaHostObject> eaHostObjectList = eaHostObjectMapper.selectByMap(eaHostObjectMap);
			List<Map<String,Object>> eaHostObjectMapList = new ArrayList<Map<String,Object>>();
			String hostObjectName = "";
			if(eaHostObjectList != null && eaHostObjectList.size() > 0){
				for (int i = 0; i < eaHostObjectList.size(); i++) {
					Map<String,Object> eaHostObjectMap2 = JSON.parseObject(JSON.toJSONString(eaHostObjectList.get(i), SerializerFeature.WriteMapNullValue), Map.class);
					//创建人
					eaHostObjectMap2.put("createByName",changeIdToNameUtil.getNameById(EmptyUtil.checkNull(eaHostObjectMap2.get("createBy"))));
					//更新人
					eaHostObjectMap2.put("updateByName",changeIdToNameUtil.getNameById(EmptyUtil.checkNull(eaHostObjectMap2.get("updateBy"))));
					//所属部门
					eaHostObjectMap2.put("sysOrgCodeName",changeIdToNameUtil.getNameByDepartId(EmptyUtil.checkNull(eaHostObjectMap2.get("sysOrgCode"))));

					//格式化时间
					Long createTime1 = eaHostObjectMap2.get("createTime") != null ? Long.parseLong(eaHostObjectMap2.get("createTime").toString()) : null;
					String createTime1Str = DateFormatUtil.formatDateyyMMddHHmmss(createTime1);
					eaHostObjectMap2.put("createTime",createTime1Str);

					//格式化时间
					Long updateTime1 = eaHostObjectMap2.get("updateTime") != null ? Long.parseLong(eaHostObjectMap2.get("updateTime").toString()) : null;
					String updateTime1Str = DateFormatUtil.formatDateyyMMddHHmmss(updateTime1);
					eaHostObjectMap2.put("updateTime",updateTime1Str);

					//添加借款单明细
					eaHostObjectMapList.add(eaHostObjectMap2);

					// 获取接待对象并拼接成字符串
					hostObjectName += "" + eaHostObjectMap2.get("hostObject") + ",";
				}
			}

			//将明细信息加入返回节点
			returnMap.put("eaHostObjectList",eaHostObjectMapList);
			returnMap.put("hostObjectName",hostObjectName.substring(0,hostObjectName.length()-1));

			//添加相应的活动明细
			Map<String,Object> activitiParams = new HashMap<String,Object>();
			activitiParams.put("pid",id);
			List<EaEntertainReceptionActivity> eaEntertainReceptionActivities = eaEntertainReceptionActivityMapper.selectByMap(activitiParams);
			List<Map<String,Object>> mapList6 = new ArrayList<Map<String,Object>>();
			if(eaEntertainReceptionActivities.size() > 0){
				for (EaEntertainReceptionActivity eaEntertainReceptionActivity: eaEntertainReceptionActivities) {
					Map map6 = JSON.parseObject(JSON.toJSONString(eaEntertainReceptionActivity, SerializerFeature.WriteMapNullValue), Map.class);

					//格式化时间
					Long date = map6.get("date") != null ? Long.parseLong(map6.get("date").toString()) : null;
					String dateStr = DateFormatUtil.formatDateyyMMdd(date);
					map6.put("date",dateStr);

					mapList6.add(map6);
				}
			}

			//将活动明细加到eaEntertainReceptionActivities节点
			returnMap.put("eaEntertainReceptionActivities",mapList6);

			Map<String,Object> eaPhoneRecordMap = new HashMap<String,Object>();
			eaPhoneRecordMap.put("entertain_apply_id",id);
			List<EaPhoneRecord> eaPhoneRecords = eaPhoneRecordMapper.selectByMap(eaPhoneRecordMap);
			EaPhoneRecord eaPhoneRecord = new EaPhoneRecord();
			if(eaPhoneRecords.size()>0){
				 eaPhoneRecord = eaPhoneRecords.get(0);
				String acceptDept = eaPhoneRecord.getAcceptDept();
				String acceptDeptName = changeIdToNameUtil.getNameByDepartId(acceptDept);
				eaPhoneRecord.setAcceptDeptName(acceptDeptName);
			}
			returnMap.put("eaPhoneRecord",eaPhoneRecord);
		}
		return returnMap;
	}

	@Override
	@Transactional
	public Map<String, Object> audit(EaTaskVo eaTaskVo) {
		Map<String,Object> returnMap = new HashMap<String,Object>();
		String id = EmptyUtil.checkNull(eaTaskVo.getId());   //单据id
//        String auditManId = EmptyUtil.checkNull(eaTaskVo.getAuditManId());   //审批人id
//        String auditMan = EmptyUtil.checkNull(eaTaskVo.getAuditMan());   //审批人
		String auditComment = EmptyUtil.checkNull(eaTaskVo.getAuditComment());   //审批意见
		String status = EmptyUtil.checkNull(eaTaskVo.getStatus());   //单据状态:2通过,3退回

		//获取招待费申请单-更新状态
		EaEntertainApply eaEntertainApply = eaEntertainApplyMapper.selectById(id);
		eaEntertainApply.setState(Integer.parseInt(status));
		eaEntertainApplyMapper.updateById(eaEntertainApply);

		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("form_id",eaEntertainApply.getId());
		List<EaTask> eaTaskList = eaTaskMapper.selectByMap(paramsMap);
		if(eaTaskList != null && eaTaskList.size() > 0){
			EaTask eaTask = eaTaskList.get(0);
			//eaTask.setAuditManId(auditManId);
			//eaTask.setAuditMan(auditMan);
			eaTask.setAuditComment(auditComment);
			eaTask.setStatus(status);

			try {
				if(status.equals("3") || status.equals("2")){
					eaTask.setFinishTime(new Date());   //完成时间
				}
				eaTask.setAuditTime(new Date());    //审批时间
				eaTaskMapper.updateById(eaTask);

				returnMap.put("code","0");
				returnMap.put("message","审核成功!");
				return returnMap;
			}catch (Exception e){
				returnMap.put("code","-1");
				returnMap.put("message","系统异常,审核失败!");
				return returnMap;
			}
		}else{
			returnMap.put("code","-1");
			returnMap.put("message","待办不存在!");
			return returnMap;
		}
	}

}
